package com.dehui.traders.ui.predict;

import android.databinding.ObservableField;
import android.text.TextUtils;

import com.dehui.traders.R;
import com.dehui.traders.bean.BaseListResponse;
import com.dehui.traders.bean.PredictBean;
import com.dehui.traders.service.NetImpl;
import com.dehui.traders.utils.DateUtil;
import com.dehui.traders.utils.UserHelp;
import com.dehui.traders.widgets.BaseViewHolder;
import com.dehui.traders.widgets.CommonAdapter;
import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.utils.Utils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PredictViewModel extends BaseViewModel{

    public List<PredictBean> list = new ArrayList<>();
    public ObservableField<String> previoutprice = new ObservableField<>("00.00%");
    public ObservableField<String> maimai = new ObservableField<>("00.00%");

    public CommonAdapter Adapter = new CommonAdapter<PredictBean>(context,list,
            R.layout.list_item_predict){

        @Override
        public void convert(BaseViewHolder helper, PredictBean item, int position) {
            String date = DateUtil.getStringByFormat(item.getDate(), DateUtil.dataFormatMDhm);
            helper.setText(R.id.tv_time,date);
            helper.setText(R.id.tv_type_name,"1".equals(item.getLabel()) ? "买入":"卖出");
            helper.setText(R.id.tv_price,item.getPrice());
            helper.setText(R.id.tv_difference_price,item.getDifferencePreviousPrice());
            helper.setText(R.id.tv_previous_date,item.getPreviousDate());
            helper.setText(R.id.tv_trade_type,"-1".equals(item.getLabel()) ? "买入":"卖出");
            helper.setText(R.id.tv_mai_dian,item.getPreviousAverage());
            helper.setText(R.id.tv_mai_dian_dif,item.getDifferencePreviousAverage());
        }
    };

    public ObservableField<CommonAdapter> AdapterObservable = new ObservableField<>(Adapter);
    private final String code;


    public PredictViewModel(PredictActivity context) {
        super(context);
        code = context.getIntent().getStringExtra("code");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        init();
    }

    private void init() {
        Call<BaseListResponse<PredictBean>> call = NetImpl.getNextLalTabel(code, UserHelp.getUserId());
        call.enqueue(new Callback<BaseListResponse<PredictBean>>() {
            @Override
            public void onResponse(Call<BaseListResponse<PredictBean>> call, Response<BaseListResponse<PredictBean>> response) {
                BaseListResponse<PredictBean> body = response.body();
                if (body == null){
                    return;
                }
                if (body.status == 200){
                    List<PredictBean> alist = body.result;
                    list.clear();
                    list.addAll(alist);
                    Adapter.notifyDataSetChanged();
                    dealTotal(list);
                }
            }

            @Override
            public void onFailure(Call<BaseListResponse<PredictBean>> call, Throwable t) {

            }
        });

    }

    private void dealTotal(List<PredictBean> list) {
        double total = 0;
        for (PredictBean item : list){
            String dp = item.getDifferencePreviousPrice();
            if (TextUtils.isEmpty(dp)){
                continue;
            }
            try {
                String ss = dp.replace("%", "");
                double doub = Double.valueOf(ss);
                total += doub;
            }catch (Exception e){

            }
        }
        previoutprice.set(Utils.doubleToString(total) + "%");
        double total1 = 0;
        for (PredictBean item : list){
            String dp = item.getDifferencePreviousAverage();
            if (TextUtils.isEmpty(dp)){
                continue;
            }
            try {
                String ss = dp.replace("%", "");
                double doub = Double.valueOf(ss);
                total1 += doub;
            }catch (Exception e){

            }
        }
        maimai.set(Utils.doubleToString(total1) + "%");

    }
}
